package com.yunhe.assist.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yunhe.assist.domain.FeedbackReply;
import com.yunhe.assist.enums.EnumFeedbackUserType;
import com.yunhe.assist.enums.EnumReplyStat;
import com.yunhe.assist.repository.FeedbackReplyRepository;
import com.yunhe.assist.service.FeedbackReplyService;
import com.yunhe.assist.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description feedback_reply
 * @author dlz
 * @date 2021-11-09
 */
@Service
public class FeedbackReplyServiceImpl implements FeedbackReplyService {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private FeedbackReplyRepository feedbackReplyRepository;

    @Transactional
    @Override
    public int createFeedbackReply (FeedbackReply feedbackReply) {
        Assert.isTrue(StrUtil.isNotBlank(feedbackReply.getReplyName()), "回复人账号不能为空");
        Assert.isTrue(StrUtil.isNotBlank(feedbackReply.getReplyContent()), "回复内容不能为空");
        Assert.isTrue(Objects.nonNull(feedbackReply.getFeedbackId()), "反馈表id不能为空");
        //更新主表回复状态
        feedbackService.updateReplyStat(feedbackReply.getFeedbackId(), EnumReplyStat.YES.getKey());
        //创建记录
        feedbackReply.setCreateTime(new Date());
        return feedbackReplyRepository.createFeedbackReplySelective(feedbackReply);
    }


    @Override
    public int deleteFeedbackReply(int id) {
        return feedbackReplyRepository.deleteFeedbackReplyByPrimaryKey(id);
    }


    @Override
    public int updateFeedbackReply(FeedbackReply feedbackReply) {
        return feedbackReplyRepository.updateFeedbackReplyByPrimaryKeySelective(feedbackReply);
    }


    @Override
    public FeedbackReply findFeedbackReplyById(int id) {
        return feedbackReplyRepository.searchFeedbackReplyByPrimaryKey(id);
    }

    @Override
    public List<FeedbackReply> findListByFeedbackId(long feedbackId) {
        return feedbackReplyRepository.findListByFeedbackId(feedbackId);
    }

    @Override
    public FeedbackReply getFirstByFeedbackId(long feedbackId) {
        return feedbackReplyRepository.getFirstByFeedbackId(feedbackId);
    }


}